window.onload = function () {
  var url = '/banner/banner-index';
  var config = {
        elem: "#banner",
        url: url,
        cols: [[
              {field:'id', width:80, title: 'id', sort: true}
              ,{field:'type', width:120, title: '跳转目标',templet:function (item) {
                    if(parseInt(item.type) == 1){
                        return '活动';
                    }else{
                        return '商家';
                    }
                }}
              ,{field:'img_url', width:160, title: '图片',  templet:'#header'}
              ,{field:'is_del', width:120, title: '是否显示', templet:function (d) {
                    var checked = d.is_del == 0 ? 'checked' : '';
                    return '<input type="checkbox" name="newsTop" data-id="'+d.id +'" '+checked+' lay-filter="newsTop" lay-skin="switch" lay-text="是|否" '+d.newsTop+'>'
                }}
              ,{field:'show_platform', width:110, title: '显示平台',templet:function (item) {
                    if(parseInt(item.show_platform) == 1){
                        return 'B端';
                    }else{
                        return 'C端';
                    }
                }}
              ,{field:'create_time', width:170, title: '创建时间',templet:function (item) {
                   return timestampToTime(parseInt(item.create_time));

                }}
              ,{field:'update_time', title: '更新时间', width: 170,templet:function (item) {
                    return timestampToTime(parseInt(item.update_time));

                }}
              ,{fixed: 'right', width:150, title:'操作', align:'center', toolbar: '#barDemo'} //这里的toolbar值是模板元素的选择器

        ]]
      };
    layui.use(['form','layer','jquery','table'], function(){
        var table = layui.table,
            form = layui.form,
            layer = layui.layer,
            $ = layui.jquery,
            operation = {
                "bannerDel":function (data) {
                    Utils.layer.closeAll();
                    Utils.layer.load(1,{'shade':0.5});
                    $.ajax({
                        url: '/banner/banner-del',
                        data:data,
                        method:'post',
                        dataType:'json',
                        success:function (data) {
                            Utils.layer.closeAll();
                            Utils.layer.msg(data.msg);
                            table.render(config);
                        }
                    });
                    return true;
                },

            };
        table.render(config);
        //监听工具条
        table.on('tool(barDemo)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            switch (layEvent){
                case 'edit':
                    location.href = '/banner/banner-update?id='+data.id;
                    break;
            }
        });
        //是否显示
        form.on('switch(newsTop)', function(obj){
            var is_del = 1;
            if($(obj.elem).is(':checked')){
                is_del = 0;
            }
            var data = obj.elem;
            var id = data.getAttribute('data-id');
            operation.bannerDel({
                id: id,
                is_del: is_del
            })
        });
    });
};
